import request from "@/utils/request";
import type { AxiosPromise } from "axios";
import type { DeptVO } from "./../dept/types";
import type { UserForm, UserInfoVO, UserQuery, UserVO } from "@/api/system/user/types";
import { parseStrEmpty } from "@/utils/asiainfo";

/**
 * 查询用户列表
 * @param query
 */
export const listUser = (query: UserQuery): AxiosPromise<UserVO[]> => {
  return request({
    url: "/system/user/list",
    method: "get",
    params: query
  });
};

/**
 * 查询部门下拉树结构
 */
export const deptTreeSelect = (): AxiosPromise<DeptVO[]> => {
  return request({
    url: "/system/user/deptTree",
    method: "get"
  });
};

/**
 * 获取用户详情
 * @param userId
 */
export const getUser = (userId?: string | number): AxiosPromise<UserInfoVO> => {
  return request({
    url: "/system/user/" + parseStrEmpty(userId),
    method: "get"
  });
};

/**
 * 用户密码重置
 * @param userId 用户ID
 * @param password 密码
 */
export const resetUserPwd = (userId: string | number, password: string) => {
  const data = {
    userId,
    password
  };
  return request({
    url: "/system/user/resetPwd",
    method: "put",
    headers: {
      isEncrypt: true,
      repeatSubmit: false
    },
    data: data
  });
};

/**
 * 修改用户
 */
export const updateUser = (data: UserForm) => {
  return request({
    url: "/system/user",
    method: "put",
    data: data
  });
};

/**
 * 删除用户
 * @param userId 用户ID
 */
export const delUser = (userId: Array<string | number> | string | number) => {
  return request({
    url: "/system/user/" + userId,
    method: "delete"
  });
};

/**
 * 新增用户
 */
export const addUser = (data: UserForm) => {
  return request({
    url: "/system/user",
    method: "post",
    data: data
  });
};

/**
 * 通过id 列表获取用户基础信息
 * @returns
 */
export const getUserInfoByIds = (userIds: Array<string | number>) => {
  return request({
    url: "system/user/getUserInfoByIds/" + userIds,
    method: "get"
  });
};

export default {
  listUser,
  updateUser,
  addUser,
  delUser,
  resetUserPwd,
  getUser,
  deptTreeSelect
};
